package com.ruitu.mall_98_2.order.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.beanu.arad.Arad;
import com.beanu.arad.base.my_base.Config;
import com.beanu.arad.base.my_base.MyBaseActivity;
import com.beanu.arad.utils.TimeUtils;
import com.beanu.arad.utils.ToastUtils;
import com.beanu.arad.widget.progressactivity.ProgressLayout;
import com.bumptech.glide.Glide;
import com.ruitu.mall_98_2.R;
import com.ruitu.mall_98_2.dao.APIRetrofit;
import com.ruitu.router_module.bean.api.BaseResult;
import com.ruitu.router_module.bean.api.SubscriberImpl;
import com.ruitu.router_module.bean.order.OrderDetail;
import com.ruitu.router_module.bean.order.SmallOrder;

import java.util.ArrayList;
import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class AfterSalesDetailActivity extends MyBaseActivity {
    private TextView tv_return_order_status;//订单的退款状态
    private TextView tv_order_return_money;//订单退款金额
    private TextView tv_company_name;//企业名称
    private ImageView iv_company_logo;//企业logo
    private ImageView iv_after_sales_type;//售后状态
    private TextView tv_after_sales_type;//售后类型
    private TextView tv_reciver_type;//收货状态
    private TextView tv_return_money;//退款金额
    private TextView tv_return_reason;//退款原因
    private TextView tv_commodity_name;//商品名称
    private TextView tv_order_number;//订单编号
    private TextView tv_creat_time;//申请时间
    private TextView tv_pay_type;//支付方式
    private TextView tv_return_path;//退款渠道
    private TextView tv_return_status;//退款状态

    private String id;//订单id
    private List<Integer> skipId = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_after_sales_detail;
    }

    @Override
    protected ProgressLayout findProgressLayout() {
        return findViewById(R.id.progress);
    }

    @Override
    protected void initViews() {
        super.initViews();
        setHeadTitle("退款详情");
        skipId.add(R.id.header);
        showEmptyView(skipId);
        id = getIntent().getStringExtra("id");
        tv_return_order_status = findViewById(R.id.tv_return_order_status);
        tv_order_return_money = findViewById(R.id.tv_order_return_money);
        tv_company_name = findViewById(R.id.tv_company_name);
        iv_company_logo = findViewById(R.id.iv_company_logo);
        iv_after_sales_type = findViewById(R.id.iv_after_sales_type);
        tv_after_sales_type = findViewById(R.id.tv_after_sales_type);
        tv_reciver_type = findViewById(R.id.tv_reciver_type);
        tv_return_money = findViewById(R.id.tv_return_money);
        tv_return_reason = findViewById(R.id.tv_return_reason);
        tv_commodity_name = findViewById(R.id.tv_commodity_name);
        tv_order_number = findViewById(R.id.tv_order_number);
        tv_creat_time = findViewById(R.id.tv_creat_time);
        tv_pay_type = findViewById(R.id.tv_pay_type);
        tv_return_path = findViewById(R.id.tv_return_path);
        tv_return_status = findViewById(R.id.tv_return_status);
        getData();
    }

    private void initData(OrderDetail orderDetail) {
        SmallOrder orderDetails = orderDetail.getOrderDetails();
        if (TextUtils.equals(orderDetails.getOrderStatus(), "8")) {
            tv_return_order_status.setText("退款成功");
            tv_return_status.setText("退款成功");
            iv_after_sales_type.setBackgroundResource(R.mipmap.icon_suc);
        } else {
            tv_return_order_status.setText("申请退款中");
            tv_return_status.setText("申请退款中");
            iv_after_sales_type.setBackgroundResource(R.mipmap.icon_waiting);
        }

        tv_order_return_money.setText("￥" + orderDetails.getOrder().getOrderTotal());
        tv_company_name.setText(orderDetails.getCompanyName());
        Glide.with(this).load(Config.BASE_URL).apply(Arad.opts_shop).into(iv_company_logo);
        tv_after_sales_type.setText("仅退款");
        if (TextUtils.equals(orderDetails.getRefundStatus(), "0")) {
            tv_reciver_type.setText("未收到货");
        }else {
            tv_reciver_type.setText("已收到货");
        }
        tv_return_money.setText(orderDetails.getOrder().getOrderTotal()+"元");
        tv_return_reason.setText(orderDetails.getRefundReason());
        tv_commodity_name.setText(orderDetails.getGoodsName());
        tv_order_number.setText(orderDetails.getId());
        tv_creat_time.setText(TimeUtils.millis2String(orderDetails.getCreateTime()));
        if (TextUtils.equals(orderDetails.getPaymentType(),"0")){
            tv_pay_type.setText("微信");
        }else if (TextUtils.equals(orderDetails.getPaymentType(),"1")){
            tv_pay_type.setText("支付宝");
        }else {
            tv_pay_type.setText("余额");
        }

    }

    //获取订单详情
    public void getData() {
        APIRetrofit.getDefault().orderDetailsInfo(id)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io()).subscribe(new SubscriberImpl<BaseResult<OrderDetail>>() {
            @Override
            public void onError(Throwable e) {
                ToastUtils.showShort("获取详情失败");
                e.printStackTrace();
                showError(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        getData();
                    }
                });
            }

            @Override
            public void onNext(BaseResult<OrderDetail> orderDetailBaseResult) {
                if (orderDetailBaseResult.isSuccess()) {
                    if (orderDetailBaseResult != null) {
                        showContent(null);
                        initData(orderDetailBaseResult.getData());
                    } else {
                        showError(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                getData();
                            }
                        });
                    }
                } else {
                    showError(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            getData();
                        }
                    });
                }
            }
        });
    }
}
